//
//  BuyTicketHeaderView.m
//  didacar
//
//  Created by imac on 15/12/1.
//  Copyright © 2015年 dzmmac. All rights reserved.
//

#import "BuyTicketHeaderView.h"
#import "UIScreen+Frame.h"
#import "UIButton+Dzm.h"
#import "GTCommontHeader.h"
#import "AppConfig.h"
#import "HudManager.h"
@implementation BuyTicketHeaderView

+(instancetype)getInstance{
    return [[BuyTicketHeaderView alloc ] init];
}

-(id)init{
    NSArray *array = [[NSBundle mainBundle ] loadNibNamed:@"BuyTicketHeaderView" owner:self options:nil];
    self = array[0];
    [self confiyStyle];
    [self configBtnAction];
    return [super init];
}

-(void)confiyStyle{
    self.backgroundColor = GLOBLE_BACKGROUND_COLOR;
    [self.searchRoadLineBtn setRadius:6];
    self.bgView.layer.cornerRadius = 6;
    self.bgView.layer.borderColor = GLOBLE_GRAY_COLOR_1.CGColor;
    self.bgView.layer.borderWidth = 0.45;
    self.bgView.layer.masksToBounds = YES;
    
   
    self.timeBgView.layer.cornerRadius = 6;
    self.timeBgView.layer.borderColor = GLOBLE_GRAY_COLOR_1.CGColor;
    self.timeBgView.layer.borderWidth = 0.45;
    self.timeBgView.layer.masksToBounds = YES;
    
    self.line_height.constant = 0.45;
   
    self.frame = CGRectMake(0, 0, [UIScreen width], 200);
    
    self.startTextField.delegate = self;
    self.endTextField.delegate   = self;
    self.timeTextField.delegate  = self;
    self.startClear = YES;
    self.endClear = YES;
    self.timeClear = YES;
    [self.startTextField setValue:GLOBLE_GRAY_COLOR_2 forKeyPath:@"_placeholderLabel.textColor"];
    [self.endTextField   setValue:GLOBLE_GRAY_COLOR_2 forKeyPath:@"_placeholderLabel.textColor"];
    [self.timeTextField   setValue:GLOBLE_GRAY_COLOR_2 forKeyPath:@"_placeholderLabel.textColor"];
//    if (MOBILEMODEL_GREATER_THAN_OR_EQUAL_TO_6) {
//        self.bg_height.constant = 118;
//        self.roadLine_height.constant = 47;
//        self.frame = CGRectMake(0, 0, [UIScreen width], 225);
//    }else{
//        self.bg_height.constant = 100;
//        self.roadLine_height.constant = 40;
//        
//    }
}

- (BOOL)textFieldShouldClear:(UITextField *)textField{
    if (textField.tag == 1) {
        self.startClear = NO;
    }else if (textField.tag == 2) {
        self.endClear = NO;
    }else {
        self.timeClear = NO;
    }
    return YES;
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    
    // 开始点
    if (textField.tag == 1) {
        if (self.startClear && self.BtnBlock) {
            
            self.BtnBlock(textField.tag);
        }
        self.startClear = YES;
    }
    // 结束点
    else if (textField.tag == 2) {
        if (self.endClear && self.BtnBlock) {
           
            self.BtnBlock(textField.tag);
        }
         self.endClear = YES;
    }else{
        if (self.timeClear && self.BtnBlock) {
            
            self.BtnBlock(textField.tag);
        }
        self.timeClear = YES;
        
    }
    return NO;
}

-(void)configBtnAction{
    
    // 交换地点
    [self.changeBtn addActionHandler:^(UIButton *btn) {
       
        if ([self exchangeLocation] && self.BtnBlock) {
            self.BtnBlock(btn.tag);
        }
    }];
    
    // 查询路线
    [self.searchRoadLineBtn addActionHandler:^(UIButton *btn) {
        if (self.BtnBlock ) {
            self.BtnBlock(btn.tag);
        }
    }];
}

-(void)setAlertStyle:(NSInteger)alertStyle{
    
    _alertStyle = alertStyle;
    self.timeBgView.hidden = YES;
    if (alertStyle == 1) {
        self.startTextField.placeholder = @"出发地";
        self.endTextField.placeholder   = @"目的地";
    }else if (alertStyle == 2){
        self.startTextField.placeholder = @"出发地区域";
        self.endTextField.placeholder   = @"目的地区域";
    }else if (alertStyle == 3){
        self.startTextField.placeholder = @"出发地";
        self.endTextField.placeholder   = @"目的地";
        self.searchRoadLineBtn.hidden = YES;
        self.timeBgView.hidden = NO;
    }
}

-(BOOL)exchangeLocation{
    
    if (self.alertStyle == 1 || self.alertStyle == 3) {
        if ([self.startTextField.text isEqualToString:@""]) {
            [HudManager showFailure:@"请选择出发地"];
            return NO;
        }
        if ([self.endTextField.text isEqualToString:@""]) {
            [HudManager showFailure:@"请选择目的地"];
            return NO;
        }
        
        NSString *startString = self.startTextField.text;
        self.startTextField.text  = self.endTextField.text;
        self.endTextField.text    = startString;
        return YES;
    }else if (self.alertStyle == 2){
        if ([self.startTextField.text isEqualToString:@""]) {
            [HudManager showFailure:@"请选择出发地区域"];
            return NO;
        }
        if ([self.endTextField.text isEqualToString:@""]) {
            [HudManager showFailure:@"请选择目的地区域"];
            return NO;
        }
        
        NSString *startString = self.startTextField.text;
        self.startTextField.text  = self.endTextField.text;
        self.endTextField.text    = startString;
        return YES;
    }
    return NO;
}

-(BOOL)validata{
    if (self.alertStyle == 1 || self.alertStyle == 3) {
        if ([self.startTextField.text isEqualToString:@""]) {
            [HudManager showFailure:@"请选择出发地"];
            return NO;
        }
        if ([self.endTextField.text isEqualToString:@""]) {
            [HudManager showFailure:@"请选择目的地"];
            return NO;
        }
        return YES;
    }else if (self.alertStyle == 2){
        if ([self.startTextField.text isEqualToString:@""]) {
            [HudManager showFailure:@"请选择出发地区域"];
            return NO;
        }
        if ([self.endTextField.text isEqualToString:@""]) {
            [HudManager showFailure:@"请选择目的地区域"];
            return NO;
        }
        return YES;
    }
    return NO;
}

@end
